home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10793 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  99 lines

  1. Path: peer-news.britain.eu.net!strath-cs!st-and!usenet
  2. From: Peter Foldiak <Peter.Foldiak@st-andrews.ac.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: HELP! error(3328)"class template .. has already been defined" when using multiple files
  5. Date: Sun, 10 Mar 1996 23:33:28 +0000
  6. Organization: University of St. Andrews
  7. Message-ID: <314366C8.2F1C@st-andrews.ac.uk>
  8. NNTP-Posting-Host: banana.st-and.ac.uk
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; IRIX 5.3 IP22)
  13.  
  14. I would like to ask for some help with the following problem.
  15. (I tried to simplify the files as much as possible).
  16. When I try to compile the files below on a SG Indy I get the
  17. following error:
  18.  
  19. ----
  20. % CC main.c list.c
  21. main.c:
  22. "list.h", line 1: error(3328): class template "list" has already been
  23. defined
  24.   template<class T> class list {
  25.                           ^
  26. 1 error detected in the compilation of "main.c".
  27. list.c:
  28. %
  29. ----
  30.  
  31. If I concatenate list.c and main.c into a file called listmain.c,
  32. and compile with
  33. % CC listmain.c
  34. %
  35. the error message disapprears! So I guess it has something to do
  36. with the bits being in separate files.
  37. Also, if the files contain no templates there is no error.
  38. Could anyone please help? Any ideas?
  39. Is there something special with including headrers when I use templates?
  40. Thanks!
  41.  
  42. Peter
  43. -
  44.  
  45. ===========================================
  46. The files that won't compile:
  47.  
  48. list.h:
  49. -------
  50. template<class T> class list {
  51.         T x;
  52.         list *next;
  53. public:
  54.         list<T>::list();
  55. };
  56.  
  57. list.c:
  58. -------
  59. #include <stdio.h>
  60. #include "list.h"
  61. template<class T> list<T>::list()
  62. {
  63.         next = NULL;
  64. }
  65.  
  66. main.c:
  67. -------
  68. #include "list.h"
  69. int main()
  70. {
  71.         list<int> q;
  72.         return 0;
  73. }
  74.  
  75.  
  76. =======================================
  77. This file compiles without error:
  78. listmain.c:
  79. -----------
  80. #include <stdio.h>
  81. #include "list.h"
  82.  
  83. template<class T> list<T>::list()
  84. {
  85.         next = NULL;
  86. }
  87.  
  88. int main()
  89. {
  90.         list<int> q;
  91.         return 0;
  92. }
  93.  
  94. -- 
  95. Peter Foldiak                     http://psych.st-and.ac.uk:8080/~pf2
  96. Psychological Laboratory          phone: +44 1334 462087
  97. University of St Andrews          fax:   +44 1334 463042
  98. St Andrews KY16 9JU, U.K.         e-mail: Peter.Foldiak@st-and.ac.uk
  99.